﻿using Bogus;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace WpfApp;

public class FakeJsonConverter
{
    public static string GenerateFakeData(string jsonTemplate)
    {

        // 校验JSON格式
        if (string.IsNullOrEmpty(jsonTemplate))
        {
            throw new ArgumentNullException(nameof(jsonTemplate), "JSON字符串不能为空");
        }


        try
        {
            var faker = new Faker();

            var jToken = JToken.Parse(jsonTemplate);

            void ProcessToken(JToken token)
            {
                switch (token.Type)
                {
                    case JTokenType.Object:
                        foreach (var prop in ((JObject)token).Properties())
                            ProcessToken(prop.Value);
                        break;

                    case JTokenType.Array:
                        var array = token as JArray;
                        if (array.Count > 0)
                        {
                            // 保持数组原有长度，复制第一个元素的结构
                            var template = array[0].DeepClone();
                            array.Clear();
                            for (int i = 0; i < faker.Random.Int(1, 5); i++)
                            {
                                var newItem = template.DeepClone();
                                ProcessToken(newItem);
                                array.Add(newItem);
                            }
                        }

                        break;

                    case JTokenType.String:
                        ((JValue)token).Value = GenerateRandomString();
                        break;

                    case JTokenType.Integer:
                        ((JValue)token).Value = faker.Random.Int(1, 1000);
                        break;

                    case JTokenType.Float:
                        ((JValue)token).Value = faker.Random.Double(0, 1000);
                        break;

                    case JTokenType.Boolean:
                        ((JValue)token).Value = faker.Random.Bool();
                        break;

                    case JTokenType.Date:
                        ((JValue)token).Value = faker.Date.Past();
                        break;

                    case JTokenType.Null:
                        // 随机决定是否保持null
                        if (faker.Random.Bool())
                        {
                            token.Replace(new JValue(GenerateRandomString()));
                        }

                        break;
                }
            }


            string GenerateRandomString()
            {
                var randomTypes = new Func<string>[]
                {
                () => faker.Name.FullName(),
                () => faker.Internet.Email(),
                () => faker.Phone.PhoneNumber(),
                () => faker.Internet.Url(),
                () => faker.Address.FullAddress(),
                () => faker.Company.CompanyName(),
                () => faker.Lorem.Sentence(),
                () => faker.Commerce.ProductName(),
                () => faker.Date.Past().ToString("yyyy-MM-dd"),
                () => faker.Finance.Amount().ToString(),
                () => faker.Internet.IpAddress().ToString(),
                () => faker.Internet.UserName()
                };

                return randomTypes[faker.Random.Int(0, randomTypes.Length - 1)]();
            }

            ProcessToken(jToken);
            return jToken.ToString();
        }
        catch (JsonReaderException)
        {

            return jsonTemplate;
        }

    }
}